<?php 
session_start();

if (!isset($_SESSION['mobile'])) {
	echo "<script>window.location.href='../view/login.php';</script>";
	$_SESSION['userurl'] = $_SERVER['REQUEST_URI'];
	exit;
}
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<link rel="stylesheet" href="./css/index.css" />
	<link rel="stylesheet" href="./css/cart.css" />
	<link rel="shortcut icon" href="favicon.ico" />
	<title>购物车 - OneCake自由一客官网 - 送到家的天然法式蛋糕，东莞蛋糕24小时网上在线预定，免费送货上门。</title>
	<style>
		.img a img{
			width:80px
		}
	</style>
</head>
<body>

	<?php 
		require('./header.php');
	 ?>
	 	<div class="backToTop" id="goToTop" style="right:9.5px; display:none;">
			<ul>
				<li class="bstop" id="to_top"></li>
			</ul>
		</div>
		<script>
			$(function scrollToTop(){
				$(window).scroll(function(){
					if($(window).scrollTop()>200){
						$("#goToTop").fadeIn(200);//显示按钮
					}else{
						$("#goToTop").fadeOut(200);//当滚动到顶部时，隐藏按钮
					}
				});
				$("#to_top").click(function(){
					$('body').animate({scrollTop:0},200);//点击按钮时，滚动到顶部
					return false;
				});
			});

		</script>
<section>
	<div class="w">
	<div class="shoppingcar">
			<h3>购物车</h3>
			<ul>
				<li id="focus" class="step-1">1、我的购物车</li>
				<li>2、填写核对订单信息</li>
				<li>3、成功提交订单</li>
			</ul>
	</div>

	<div style="display: none;" id="m_noGoods" class="nocart">
		<div>购物车内暂时没有商品</div>
		<div>去<a href="list_1_1_1.php?id=0">产品名录</a>挑选</div>
	</div>

	<div id="goods_cartlist" class="cartlist">
		<ul class="title">
			<li class="t-checkbox">
				<input type="checkbox" class="checkbox checkall" id="chkall_1" checked="">
				<label for="chkall">全选</label>
			</li>
			<li class="t-goods">商品名称</li>
			<li class="t-spec">规格</li>
			<li class="t-price">单价(元)</li>
			<li class="t-stock">数量</li>
			<li class="t-sale">优惠</li>
			<li class="t-total">小计(元)</li>
			<li class="t-ope">操作</li>
		</ul>
		<form style="display: block;" action="">
			<div id="goods_list" class="lis">
<!-- 				<ul class="item clearfix">
					<li class="t-checkbox">
						<input type="checkbox" class="checkbox" id="chk_1" checked="">
					</li>

					<li class="t-goods clearfix">
						<div class="img fl">
							<a href="###" target="_blank">
								<img src="images/243755769611288838911bca28b3ae5b.jpg_200x200.jpg" width="80px">
							</a>
						</div>
						<div class="info fl">
								<a href="###" target="_blank">芒果拿破仑蛋糕
									<span style="color:#999; padding-left:5px;">Mango Napoleon Cake</span>
								</a>
						</div>
					</li>

					<li class="t-spec clearfix">
						<span>1磅</span>
					</li>
					<li class="t-price">188.00</li>
					<li class="t-stock">
						<div class="number">
							<input type="text" value="1" id="num" class="txt"/>
							<span class="del">-</span>
							<span class="add">+</span>
						</div>
					</li>
					<li class="t-sale">-</li>
					<li class="t-total">188.00</li>
					<li class="t-ope"><a href="javascript:;">删除</a></li>
				</ul> -->
<!-- 				<ul class="item clearfix">
					<li class="t-checkbox">
						<input type="checkbox" class="checkbox" id="chk_2" checked="">
					</li>

					<li class="t-goods clearfix">
						<div class="img fl">
							<a href="###" target="_blank">
								<img src="images/0e326c3c0a4ccbfc4aab7c7ea8ecbcd5.jpg_200x200.jpg" width="80px">
							</a>
						</div>
						<div class="info fl">
								<a href="###" target="_blank">榴莲冰淇淋蛋糕
									<span style="color:#999; padding-left:5px;">Durian Ice cream Cake</span>
								</a>
						</div>
					</li>

					<li class="t-spec clearfix">
						<span>1磅</span>
					</li>
					<li class="t-price">188.00</li>
					<li class="t-stock">
						<div class="number">
							<input type="text" value="1" id="num" class="txt"/>
							<span class="del">-</span>
							<span class="add">+</span>
						</div>
					</li>
					<li class="t-sale">-</li>
					<li class="t-total">188.00</li>
					<li class="t-ope"><a href="javascript:;">删除</a></li>
				</ul> -->
			</div>
			<div class="cart-to">
				<ul>
					<li class="t-checkbox" style="    padding-left: 15px;">
						<input type="checkbox" class="checkbox checkall" id="chkall_2" checked="">
						<label for="chkall">全选</label>
					</li>
					<li class="delk">
						<a href="javascript:;">×删除选中的商品</a>
						<a href="list_1_1_1.php?id=0">←继续购物</a>
					</li>
					<li class="tot">
						<span>总计(不含运费) : </span>
						<strong>¥</strong><strong id="total"></strong>
					</li>
					<li class="count"><a onclick="gotoOrder()" href="javascript:;">去结算</a></li>
				</ul>
			</div>
		</form>	
	</div>

	<div style="display: block;" id="peijian" class="cart-peijian">
		<h3>收费配件
			<span>（温馨提示：每磅蛋糕都有免费送6份餐具+免费蜡烛，如果您需要增加餐具和个性蜡烛可以在下面购买）</span>
		</h3>
		<table cellpadding="0" cellspacing="0">
		<tbody>
		<tr>
			<td width="85"></td>
			<td width="52">
				<a href="detail.php?id=44" target="_blank">
					<img src="images/2015070813212015690.jpg_350x350.jpg" width="80" alt="1磅大小">
				</a>
			</td>
			<td width="470" style="padding-left:10px;"><a href="detail.php?id=44" target="_blank">冷藏保温袋</a><p style="color:#999; font-size:12px;">规格：1磅大小、2磅大小</p></td>
			<td width="110" align="center">￥10.00</td>
			
			<td width="110" align="center">
				<!-- <input type="text" class="inp" value="1"> -->
			</td>
			<td align="center"><a class="buy" href="detail.php?id=44" target="_blank">加入购物车</a></td>
		</tr>
		<tr>
			<td width="85"></td>
			<td width="52">
				<a href="detail.php?id=45" target="_blank">
					<img src="images/2015040111124884078.jpg_350x350.jpg" width="80" alt="1磅大小">
				</a>
			</td>
			<td width="470" style="padding-left:10px;"><a href="detail.php?id=45" target="_blank">定制巧克力牌（画画）</a><p style="color:#999; font-size:12px;">规格：5CM内、10CM内</p></td>
			<td width="110" align="center">￥10.00</td>
			
			<td width="110" align="center">
				<!-- <input type="text" class="inp" value="1"> -->
			</td>
			<td align="center"><a class="buy" href="detail.php?id=45" target="_blank">加入购物车</a></td>
		</tr>
		<tr>
			<td width="85"></td>
			<td width="52">
				<a href="detail.php?id=46" target="_blank">
					<img src="images/2015070316160898264.jpg_350x350.jpg" width="80" alt="1磅大小">
				</a>
			</td>
			<td width="470" style="padding-left:10px;"><a href="detail.php?id=46" target="_blank">生日帽</a><p style="color:#999; font-size:12px;">规格：玫红、蓝色</p></td>
			<td width="110" align="center">￥10.00</td>
			
			<td width="110" align="center">
				<!-- <input type="text" class="inp" value="1"> -->
			</td>
			<td align="center"><a class="buy" href="detail.php?id=46" target="_blank">加入购物车</a></td>
		</tr>
		<tr>
			<td width="85"></td>
			<td width="52">
				<a href="detail.php?id=47" target="_blank">
					<img src="images/2015031809380664071.jpg_350x350.jpg" width="80" alt="1磅大小">
				</a>
			</td>
			<td width="470" style="padding-left:10px;"><a href="detail.php?id=47" target="_blank">LOVE蜡烛</a><p style="color:#999; font-size:12px;">1件</p></td>
			<td width="110" align="center">￥10.00</td>
			
			<td width="110" align="center">
				<!-- <input type="text" class="inp" value="1"> -->
			</td>
			<td align="center"><a class="buy" href="detail.php?id=47" target="_blank">加入购物车</a></td>
		</tr>
		<tr>
			<td width="85"></td>
			<td width="52">
				<a href="detail.php?id=48" target="_blank">
					<img src="images/2016121616002285932.jpg_350x350.jpg" width="80" alt="1磅大小">
				</a>
			</td>
			<td width="470" style="padding-left:10px;"><a href="detail.php?id=48" target="_blank">生日快乐字母蜡烛</a><p style="color:#999; font-size:12px;">1件</p></td>
			<td width="110" align="center">￥10.00</td>
			
			<td width="110" align="center">
				<!-- <input type="text" class="inp" value="1" > -->
			</td>
			<td align="center"><a class="buy" href="detail.php?id=48" target="_blank">加入购物车</a></td>
		</tr>
		<tr>
			<td width="85"></td>
			<td width="52">
				<a href="detail.php?id=49" target="_blank">
					<img src="images/2015011314155598165.jpg_350x350.jpg" width="80" alt="1磅大小">
				</a>
			</td>
			<td width="470" style="padding-left:10px;"><a href="detail.php?id=49" target="_blank">餐具</a><p style="color:#999; font-size:12px;">1人份</p></td>
			<td width="110" align="center">￥1.00</td>
			
			<td width="110" align="center">
				<!-- <input type="text" class="inp" value="1"> -->
			</td>
			<td align="center"><a class="buy" href="detail.php?id=49" target="_blank">加入购物车</a></td>
		</tr>


		</tbody>
		</table>
	</div>
	</div>
</section>

	<?php 
		require('./footer.php');
	 ?>
	<script>
		function initShopCart(){
			$.get('../api/shopCartApi.php',{'act':'initShopCart'},function(data){
				// alert(data.data)
				if(data.code==100){
					alert(data.msg)
					window.location.href="./login.php"
				}
				if(data.code==105){
					var jsonRows=data.data
					// alert(data.msg)
					// alert(jsonRows)
					makeLi(jsonRows)
				}
				if(data.code==104){
					$("#goods_cartlist").css("display","none")
					$("#peijian").css("display","none")
					$("#m_noGoods").css("display","block")
				}
			},'json')				
		}
		$(initShopCart)
		function makeLi(arr){
			var tab=''
			var money=0
			for(var i in arr){
				var row=arr[i]
				money+=parseFloat(row.price) * parseInt(row.num)
				// var goodname=""
				// if(row.goodname.length>20){
				// 	goodname=row.goodname.substr(0,20)+"..."
				// }
				tab+='<ul class="item clearfix">'
					+'<li class="t-checkbox">'
						+'<input onclick="checkbox(this)" type="checkbox" class="checkbox" checked="">'
					+'</li>'
					+'<li class="t-goods clearfix">'
						+'<div class="img fl">'
							+'<a href="detail.php?id='+row.goodid+'" target="_blank">'
								+htmlspecialchars_decode(row.img)
							+'</a>'
						+'</div>'
						+'<div class="info fl">'
								+'<a href="detail.php?id='+row.goodid+'" target="_blank">'
								+row.goodname
								+'</a>'
						+'</div>'
					+'</li>'
					+'<li class="t-spec clearfix">'
						+'<span name="specifications">'+row.specifications+'</span>'
					+'</li>'
					+'<li name="t-price" class="t-price">'+row.price+'</li>'
					+'<li class="t-stock">'
						+'<div id="goodNum" class="number">'
							+'<input onblur="numInputOnblur(this,'+row.goodid+')" type="text" name="qty" value="'+row.num+'" class="txt" onkeyup="onafterpaste(this)">'
							+'<span onclick ="addNum(this)" class="add">+</span>'
							+'<span onclick ="delNum(this)" class="del">-</span>'
							+'<input type="hidden" id="'+row.goodid+'" name="'+row.goodid+'" value='+row.goodid+'>'
						+'</div>'
					+'</li>'
					+'<li class="t-sale">-</li>'
					+'<li name="t-total" class="t-total">'+(parseFloat(parseFloat(row.price) * parseInt(row.num))).toFixed(2)+'</li>'
					+'<li class="t-ope"><a onclick="delGoods(this)" href="javascript:;">删除</a></li>'
				+'</ul>'
			}
			$("#total").html(parseFloat(money).toFixed(2))
			$("#goods_list").html(tab)
			$("#goods_cartlist").css("display","block")
			$("#peijian").css("display","block")
			$("#m_noGoods").css("display","none")
			initCheck()
		}

		function delNum(ts){
			//可以写成jQuery事件
			$(ts).parent().children().eq(0).focus()
			var num = parseInt($(ts).parent().children().eq(0).val())
			if(num > 1){
				num = parseInt(num)-1
				$(ts).parent().children().eq(0).val(num)
				$(total())
			}
			var goodid=$(ts).parent().children().eq(3).val()
			var specifications=$(ts).parent().parent().parent().find("span[name='specifications']").html()
			// alert(specifications)
			$.get('../api/shopCartApi.php',{'act':'changeNum','specifications':specifications,'goodid':goodid,'num':num},function(data){
			},"json")	
		}
		function addNum(ts){
			$(ts).parent().children().eq(0).focus()
			var num = parseInt($(ts).parent().children().eq(0).val())
			num = parseInt(num)+1

			var goodid=$(ts).parent().children().eq(3).val()
			var specifications=$(ts).parent().parent().parent().find("span[name='specifications']").html()
			// alert(specifications)
			$.get('../api/checkinventoryApi.php',{m:num,goodsid:goodid,specifications:specifications},function(data){
				$("#chkall_1").focus()
				if(data){
					alert(data)
					window.location.reload()
				}else{
					// alert("库存满足可以修改数量")
					$(ts).parent().children().eq(0).val(num)
					$(total())
					var goodid=$(ts).parent().children().eq(3).val()
					var specifications=$(ts).parent().parent().parent().find("span[name='specifications']").html()
					$.get('../api/shopCartApi.php',{'act':'changeNum','specifications':specifications,'goodid':goodid,'num':num},function(data){
					},"json")
				}
			},"html")			
		}
		function total(){
			var t_total = 0
			var total = 0.00
			// alert($("#goods_list").find("li[name='t-price']").length)
			for(var i = 0; i < $("#goods_list").find("li[name='t-total']").length; i++){
				var a = parseFloat($("#goods_list").find("li[name='t-price']").eq(i).html())
				var b = parseInt($("#goods_list").find("input[name='qty']").eq(i).val())
				t_total = (parseFloat(a * b)).toFixed(2)
				// alert(t_total)
				$("#goods_list").find("li[name='t-total']").eq(i).html(t_total)
				if($("#goods_list").find("input[type='checkbox']").eq(i).is(':checked') == true){
					total += a * b
				}
				
				// alert((total).toFixed(2))
			}
			$("#total").html((total).toFixed(2))
		}	

		function delGoods(ts){
			var goodid=$(ts).parent().parent().find("#goodNum").children().eq(3).attr('id')
			var specifications=$(ts).parent().parent().find("span[name='specifications']").html()
			// alert(goodid)
			$.get('../api/shopCartApi.php',{'act':'delGoods', 'specifications':specifications, 'goodid':goodid},function(data){
				if(data.code==111){
					
				}else{
					alert(data.msg)
				}
				window.location.reload()
			},"json")					
		}


		function htmlspecialchars_decode(str){           
              str = str.replace(/&amp;/g, '&'); 
              str = str.replace(/&lt;/g, '<');
              str = str.replace(/&gt;/g, '>');
              str = str.replace(/&quot;/g, '"');  
              str = str.replace(/&#039;/g, "'");  
              return str;  
    	}
		function numInputOnblur(ts,id){
			total()
			var s=$(ts).parent().parent().parent().find("span[name='specifications']").html()
			if($(ts).val()==null || $(ts).val()==""){
				alert("请输入要购买的数量")
				window.location.reload()
				return
			}
			$.get('../api/checkinventoryApi.php',{m:$(ts).val(),goodsid:id,specifications:s},function(data){
				$("#chkall_1").focus()
				if(data){
					alert(data)
					window.location.reload()
				}else{
					// alert("库存满足可以修改数量")
					var num = parseInt($(ts).val()) 
					var goodid=$(ts).parent().children().eq(3).val()
					var specifications=$(ts).parent().parent().parent().find("span[name='specifications']").html()
					$.get('../api/shopCartApi.php',{'act':'changeNum','specifications':specifications,'goodid':goodid,'num':num},function(data){
					},"json")
				}
			},"html")
		}
		function onafterpaste(ts){
			if(ts.value.length==1){
				ts.value=ts.value.replace(/[^1-9]/g,'')
			}else{
				ts.value=ts.value.replace(/\D/g,'')
			}
		}
		function initCheck(){
			for(var i=0;i<$("#goods_list").find("span[name='specifications']").length;i++){
				var s = ($("#goods_list").find("span[name='specifications']").eq(i).html())
				var id = ($("#goods_list").find("input[type='hidden']").eq(i).val())
				var num = ($("#goods_list").find("input[name='qty']").eq(i).val())
				// alert($("#goods_list").find("input[name='qty']").eq(i).val())
				$.get('../api/checkinventoryApi.php',{'act':'initCheck', i:i,m:num,goodsid:id,specifications:s},function(data){
					if(data.code==1001){
						alert("库存不足")
						inventory = parseInt(data.inventory)
						$("#goods_list").find("input[name='qty']").eq(data.i).val(inventory)
						$("#goods_list").find("input[name='qty']").eq(data.i).focus()
					}
				},"json")
			}
		}
		function checkbox(ts){
			$(total())
		}
		function gotoOrder(){
			if($("#total").html() == 0.00){
				alert("请勾选商品再去结算！")
			}else{
				window.location.href="indent.php"
			}
		}
	</script>

</body>
</html>
